﻿//This is for adding some network check codes into the FrontMgr function
//These codes will check if the device running is more than the devices number we allow client use.
//The use condition is checked by the return string of network.
//This will check if the device is full in the table, new in the table or already in the table.

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System;




public partial class FrontMeaMgr : MonoBehaviour {


	string urlStr="http://www.lvxnow.com:8080/sideshop/addRespAction!add?name=";
	//string urlStr="http://192.168.1.126:8080/sideshop/addRespAction!add?name=";
	
	
	string paraStr="paraStr";
	string WebRetStr=string.Empty;
	public static string RetStrInfo=string.Empty;
	string devName=string.Empty;
		
	static	bool enableFunction=false;
	public static bool EnableFunction {
		get {
			return enableFunction;
		}
	}

	static bool isDevCheckedThisTime=false;

	string projectname="kdxbodymea";



	
	void CheckString()
	{
		enableFunction=false;
		string lParaStr=PlayerPrefs.GetString(paraStr);
		//Debug.Log("Current ParaStr: "+lParaStr);
		//Debug.Log("Current ParaStr len:  "+ lParaStr.Length);
		if((lParaStr==null)||(lParaStr==string.Empty)||(lParaStr.Length<=1))   //Means it is the 1st time to generate this string , we need to upload and check the uuid
		{
			Debug.Log("Local Para string not exist.  Check on web");
			devName=Guid.NewGuid().ToString();
		}
		else
		{
			if(lParaStr=="Full")
			{
				enableFunction=false;
			    devName=Guid.NewGuid().ToString();
				
				Debug.LogWarning("The installed devices is outnumbered");
			}
			else
			{
				devName=PlayerPrefs.GetString(paraStr);
				enableFunction=true;
				//Debug.LogWarning("The installed devices is outnumbered");
			}
		}

		StartCoroutine("RequestDevSignal");
		
		isDevCheckedThisTime=true;
	}


	IEnumerator   RequestDevSignal(){
		
		string ldevName="\""+devName+"\"";
		ldevName = System.Uri.EscapeDataString(ldevName);
		
	    //Debug.Log("converted ldevname: "+ldevName);
		//string sendUrl=urlStr+"?"+"Name"+"="+ldevName;  //for shawn ftp, we must need ?Name=
		string sendUrl=urlStr+devName+"&"+"project"+"="+projectname;   //For lvx.  Aiden already write ?name= in string.  //do not use double quotation mark
		
		
		//		Uri uri =new Uri(sendUrl);
		//Debug.Log(sendUrl);
		
		WWW web;
		web = new WWW(sendUrl);
		yield return web;
		
		if (web.isDone && (web.error == null)){	
			//print("&&&&&&&&&&&&& bytes str: " + web.text);
			
			if (!string.IsNullOrEmpty(web.text)){
				
				WebRetStr=web.text;
				if(WebRetStr.Contains("F"))
				{
					enableFunction=false;
					PlayerPrefs.SetString(paraStr,"Full");
					RetStrInfo="The devices installed is outnumbered. Dev table is full";
					
				}
				else if((WebRetStr.Contains("I")))
				{
					enableFunction=true;
					PlayerPrefs.SetString(paraStr,devName);
					RetStrInfo="insert to table for 1st time";
				}
				else if((WebRetStr.Contains("H")))
				{
					enableFunction=true;
					RetStrInfo="Device already in the table";
				}
				else
				{
					enableFunction=false;
					RetStrInfo="Return string of Devices is not H,I or F. Network Service maybe error";
				}
				
				Debug.Log("Get return string: "+WebRetStr);
			}
			else
			{
				enableFunction=false;
				RetStrInfo="Not get any effect return string when checking network for devices number condition. Network Service maybe error";
			}
			
			Debug.Log("The device condition is : "+ RetStrInfo+"   "+ EnableFunction);

			
		}
		
	} 
	


}
